package com.administrator.customviewtest.view.floatCircleView;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/8/8.
 */
public class FloatViewManager {
    private Context context;
    private static FloatViewManager manager;
    private WindowManager wm;
    private FloatCircleView circleView;
    private   WindowManager.LayoutParams params;
    float startx,starty;
    float x2;

    private FloatViewManager(final Context context){
        this.context = context;
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        circleView = new FloatCircleView(context);
        circleView.setOnTouchListener(listener);
        circleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"ssss",Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static FloatViewManager getInstance(Context context){
        if(manager==null){
            synchronized (FloatViewManager.class){
                if(manager==null){
                    manager = new FloatViewManager(context);
                }
            }
        }
        return manager;
    }

    public void addFloatCircleView(){
        params = new WindowManager.LayoutParams();
        params.width = circleView.width;
        params.height = circleView.height;
        params.gravity = Gravity.TOP|Gravity.LEFT;
        params.x = 0;
        params.y = 0;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        params.format = PixelFormat.RGBA_8888;
        wm.addView(circleView,params);
    }

    public View.OnTouchListener listener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:{
                    startx = event.getRawX();
                    starty = event.getRawY();

                    x2 = event.getRawX();
                    break;
                }
                case MotionEvent.ACTION_MOVE:{
                    float x = event.getRawX();
                    float y  = event.getRawY();

                    float dx = x-startx;
                    float dy = y-starty;
                    params.x+=dx;
                    params.y+=dy;
                    circleView.setDragState(true);
                    wm.updateViewLayout(circleView,params);
                    startx = x;
                    starty = y;
                    break;

                }
                case MotionEvent.ACTION_UP:{
                    float x1 = event.getRawX();
                    if(x1>getScreenWidth()/2){
                        params.x=getScreenWidth()-circleView.width;
                    }else {
                        params.x = 0;
                    }
                    circleView.setDragState(false);
                    wm.updateViewLayout(circleView,params);
                    if(Math.abs(x1-x2)>6){
                        return true;
                    }else {
                        return false;
                    }
                }
                default:{
                    break;
                }
            }
            return false;
        }
    };

    public  int getScreenWidth(){
        return wm.getDefaultDisplay().getWidth();
    }

}
